package com.bocai.dahuatech.ui.activity;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.MyExchangeListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.ui.adapter.MyExchangeAdapterHolder;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的兑换
 */
public class MyExchangeActivity extends BaseActivity {
    @Bind(R.id.swipe_myexchange)
    SwipeRefreshLayout swipeMyExchange;
    @Bind(R.id.rv_myexchange)
    LoadMoreRecyclerView rvExchange;
    @Bind(R.id.ll_title_right)
    LinearLayout llTitle;
    @Bind(R.id.et_search)
    EditText etSearch;
    @Bind(R.id.ll_myexchange_nocontent)
    LinearLayout llNoContent;
    private MyExchangeAdapterHolder adapter;
    int pager=1;
    MyExchangeListBean bean;

    List<MyExchangeListBean.ContentEntity> datas=new ArrayList<>();
    @Override
    protected int contentView() {
        return R.layout.activity_my_exchange;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);

        initToolbar(R.string.meexchange);
        if(SharePrefencesUtil.getFAccessFlag(this)==2){

            llNoContent.setVisibility(View.GONE);
            llTitle.setVisibility(View.GONE);
            etSearch.setHint(R.string.productserchhint);
            rvExchange.setLayoutManager(new LinearLayoutManager(this));
            initEvent();
            getDatas();
        }else{
            llNoContent.setVisibility(View.VISIBLE);
        }
    }

    private void getDatas() {
        showLoading();
        getTestApi().getPointExchangeList(SharePrefencesUtil.getCustomerCode(this),etSearch.getText().toString(),pager+"")
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            swipeMyExchange.setRefreshing(false);
            Log.e("error", "error");
        }

        @Override
        public void onNext(BaseBean baseBean) {
            hideLoading();
            swipeMyExchange.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1"))
            {
                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                //{"content":[{"FID":304,"FItemName":"超五类网线","FOutModel":"超五类网线","FImgSrc":"/upfile/jf/20151104162935.png","FPrice":425.00,"FCoin":20000.00,"FNum":1,"FTotal":20000.00,"FExchange":true,"FGoods":false,"FCreateTime":"2015-12-19 12:01:56"},{"FID":163,"FItemName":"2A电源","FOutModel":"DH-PFM300","FImgSrc":"/upfile/jf/20150804181754.JPG","FPrice":25.00,"FCoin":1000.00,"FNum":9,"FTotal":9000.00,"FExchange":true,"FGoods":false,"FCreateTime":"2015-11-16 11:39:29"},{"FID":30,"FItemName":"2A电源","FOutModel":"DH-PFM300","FImgSrc":"/upfile/jf/20150804181754.JPG","FPrice":25.00,"FCoin":1000.00,"FNum":1,"FTotal":1000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-25 14:53:26"},{"FID":13,"FItemName":"2A电源","FOutModel":"DH-PFM300","FImgSrc":"/upfile/jf/20150804181754.JPG","FPrice":25.00,"FCoin":1000.00,"FNum":1,"FTotal":1000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-06 14:37:02"},{"FID":12,"FItemName":"硬盘","FOutModel":"ST2000VM003","FImgSrc":"/upfile/jf/20150804181836.JPG","FPrice":450.00,"FCoin":25000.00,"FNum":10,"FTotal":250000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-06 14:36:37"},{"FID":11,"FItemName":"2A电源","FOutModel":"DH-PFM300","FImgSrc":"/upfile/jf/20150804181754.JPG","FPrice":25.00,"FCoin":1000.00,"FNum":5,"FTotal":5000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-06 13:46:56"},{"FID":10,"FItemName":"硬盘","FOutModel":"ST2000VM003","FImgSrc":"/upfile/jf/20150804181836.JPG","FPrice":450.00,"FCoin":25000.00,"FNum":5,"FTotal":125000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-06 10:39:06"},{"FID":9,"FItemName":"硬盘","FOutModel":"ST2000VM003","FImgSrc":"/upfile/jf/20150804181836.JPG","FPrice":450.00,"FCoin":25000.00,"FNum":1,"FTotal":25000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-05 19:33:49"},{"FID":8,"FItemName":"网络硬盘录像机","FOutModel":"DH-NVR1104HS","FImgSrc":"/upfile/jf/20150804181827.JPG","FPrice":195.00,"FCoin":9000.00,"FNum":1,"FTotal":9000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-05 17:43:38"},{"FID":7,"FItemName":"2A电源","FOutModel":"DH-PFM300","FImgSrc":"/upfile/jf/20150804181754.JPG","FPrice":25.00,"FCoin":1000.00,"FNum":1,"FTotal":1000.00,"FExchange":true,"FGoods":true,"FCreateTime":"2015-08-04 18:20:23"}]}
                bean=new Gson().fromJson(result,MyExchangeListBean.class);
                if(pager==1){
                    adapter = new MyExchangeAdapterHolder(MyExchangeActivity.this,bean.getContent());
                    rvExchange.setAdapter(adapter);
                }else{
                    adapter.addDatas(bean.getContent());
                }
                if(bean.getContent().size()==0){
                    rvExchange.notifyMoreFinish(false);
                }else{
                    rvExchange.notifyMoreFinish(true);
                }
            }
        }
    };
    @Override
    protected void initEvent() {
        super.initEvent();
        swipeMyExchange.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pager = 1;
                datas.clear();
                getDatas();
            }
        });
        rvExchange.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                pager++;
                getDatas();
            }
        });
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId== EditorInfo.IME_ACTION_SEARCH)
                {
                    getDatas();
                }
                return false;
            }
        });
    }
}
